package com.example.smartcity_poverty.ui.AllService.RubbishClassify.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_rc_show_classify.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope

/**
 * 垃圾分类详情
 */
class RcShowClassifyActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_rc_show_classify)
        title = "垃圾分类详情"
        val content = intent.getStringExtra("content")
        val type = intent.getStringExtra("type")
        coroutine(Dispatchers.IO) {
            content?.let {
                setIdShow(content)
            }

            type?.let {
                setIdShow2(type)
            }
        }
    }

    /**
     * 填充数据
     */
    private suspend fun setIdShow(content: String) {
        api.getRcSeekAll(getToken(), content).apply {
            if (this.code == 200) {
                runOnUiThread {
                    for (i in this.rows!!){
                        glide(url + i.imgUrl, rcClassifyShowImageView)
                        rcClassifyShowTitle.text = i.name
                        rcClassifyShowContent1.text = i.introduce
                        rcClassifyShowContent2.text = "投放指导: ${i.guide}"
                    }
                }
            } else runOnUiThread { this.msg?.show() }
        }
    }

    private suspend fun setIdShow2(type: String) {
        api.getRcSeekAll2(getToken(), type).apply {
            if (this.code == 200) {
                runOnUiThread {
                    for (i in this.rows!!){
                        glide(url + i.imgUrl, rcClassifyShowImageView)
                        rcClassifyShowTitle.text = i.name
                        rcClassifyShowContent1.text = i.introduce
                        rcClassifyShowContent2.text = "投放指导: ${i.guide}"
                    }
                }
            } else runOnUiThread { this.msg?.show() }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}